﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWBetongXaydung.DAL;

namespace SWBetongXaydung.BLL
{
    class QuyetToanBLL
    {
        public static List<ThongKeTongHopThanhToan> GetList(int thang, int nam, bool isRutGon)
        {
            using (var db = DBContextHelper.GetContext())
            {
                var listQuyetToan = (from e in db.QuyetToanDbSet
                                     where e.Thang == thang &&
                                           e.Nam == nam
                                     orderby e.TaiXe.Ten ascending, e.TaiXe.HoDem ascending
                                     select e).ToList();
                List<ThongKeTongHopThanhToan> listThongKe = new List<ThongKeTongHopThanhToan>();
                if (listQuyetToan != null && listQuyetToan.Count > 0)
                {
                    //Lấy trong bảng quyết toán
                    foreach (var item in listQuyetToan)
                    {
                        ThongKeTongHopThanhToan thongKeObj = new ThongKeTongHopThanhToan();
                        thongKeObj.IDTaiXe = item.IDTaiXe;
                        thongKeObj.TenDayDu = item.TaiXe.TenDayDu;
                        thongKeObj.SoXe = item.SoXe;
                        thongKeObj.SoDuDauKy = item.SoDuDauKy;
                        thongKeObj.DaQuyetToan = item.DaQuyetToan;
                        thongKeObj.TienMat = item.ThuTienMat;
                        thongKeObj.Thang = item.Thang;
                        thongKeObj.Nam = item.Nam;
                        thongKeObj.NgayQuyetToan = item.NgayQuyetToan;

                        //var listCapNhienLieu = ThongKeCapNhienLieu.GetList(thang, nam, item.IDTaiXe, item.SoXe,isRutGon);
                        //if (listCapNhienLieu != null && listCapNhienLieu.Count > 0)
                        //    thongKeObj.SoDauTamUng = listCapNhienLieu.Sum(e => e.LuongNhienLieu);
                        //var listNhienLieuTieuThu = ThongKeNhienLieuTieuThu.GetList(thang, nam, item.IDTaiXe, item.SoXe,isRutGon);
                        //if (listNhienLieuTieuThu != null && listNhienLieuTieuThu.Count > 0)
                        //    thongKeObj.SoDauTieuThu = listNhienLieuTieuThu.Sum(e => e.TongLit);
                        //var donGiaObj = DonGiaNhienLieuBLL.GetDonGia(thang, nam, item.SoXe);
                        //if (donGiaObj != null)
                        //    thongKeObj.DonGia = donGiaObj.DonGia;
                        //thongKeObj.ThanhTien = thongKeObj.DonGia * thongKeObj.SoDauConLai;
                        //listThongKe.Add(thongKeObj);
                    }
                    return listThongKe;
                }
                else
                {
                    var _dsTaiXe = db.TaiXeDbSet.Where(e => e.DangLamViec == true).OrderBy(e => e.Ten).ThenBy(e => e.HoDem).ToList();
                    if (_dsTaiXe == null || _dsTaiXe.Count == 0)
                        return null;
                    foreach (var _taiXe in _dsTaiXe)
                    {
                        ThongKeTongHopThanhToan thongKeObj = new ThongKeTongHopThanhToan();
                        thongKeObj.IDTaiXe = _taiXe.IDTaiXe;
                        var _dsXeDuocDieu = (from e in db.DieuXeDbSet
                                             where e.IDTaiXe == _taiXe.IDTaiXe &&
                                                   e.HanhTrinh.Ngay.Month == thang &&
                                                   e.HanhTrinh.Ngay.Year == nam
                                             select new { SoXe = e.SoXe }).Distinct().ToList();
                        if (_dsXeDuocDieu == null || _dsXeDuocDieu.Count == 0)
                        {
                            //Tháng này tài xế không chạy chuyến nào

                            //Lấy xe trong phân công lái xe
                            var _pcLaiXe = (from e in db.PhanCongLaiXeDbSet
                                            where e.IDTaiXe == _taiXe.IDTaiXe
                                            orderby e.NgayNhanXe descending
                                            select e).FirstOrDefault();
                            if (_pcLaiXe == null)
                                continue;
                            else
                            {
                                thongKeObj.SoXe = _pcLaiXe.SoXe;
                                thongKeObj.DaQuyetToan = false;
                                thongKeObj.Nam = nam;
                                thongKeObj.NgayQuyetToan = DateTime.Now.Date;
                                thongKeObj.SoDuDauKy = GetSoDuDauKy(thang, nam, _taiXe.IDTaiXe, _pcLaiXe.SoXe);
                                thongKeObj.Thang = thang;
                                thongKeObj.TienMat = 0;
                                thongKeObj.TenDayDu = _taiXe.TenDayDu;
                                thongKeObj.SoDauTieuThu = 0;
                                thongKeObj.SoDauTamUng = 0;
                                thongKeObj.ThanhTien = thongKeObj.DonGia * thongKeObj.SoDauConLai;
                                QuyetToan _qt = new QuyetToan
                                {
                                    DaQuyetToan = false,
                                    IDTaiXe = thongKeObj.IDTaiXe,
                                    Nam = thongKeObj.Nam,
                                    NgayQuyetToan = thongKeObj.NgayQuyetToan,
                                    SoDuDauKy = thongKeObj.SoDuDauKy,
                                    SoXe = thongKeObj.SoXe,
                                    Thang = thongKeObj.Thang,
                                    ThuTienMat = thongKeObj.TienMat,
                                };
                                db.QuyetToanDbSet.Add(_qt);
                                listThongKe.Add(thongKeObj);
                            }
                        }
                        else
                        {
                            foreach (var _xe in _dsXeDuocDieu)
                            {
                                thongKeObj.IDTaiXe = _taiXe.IDTaiXe;
                                thongKeObj.TenDayDu = _taiXe.TenDayDu;
                                thongKeObj.SoXe = _xe.SoXe;
                                thongKeObj.SoDuDauKy = GetSoDuDauKy(thang, nam, _taiXe.IDTaiXe, _xe.SoXe);
                                thongKeObj.DaQuyetToan = false; ;
                                thongKeObj.TienMat = 0;
                                thongKeObj.Thang = thang;
                                thongKeObj.Nam = nam;
                                thongKeObj.NgayQuyetToan = DateTime.Now.Date;

                                var listCapNhienLieu = ThongKeCapNhienLieu.GetList(thang, nam, thongKeObj.IDTaiXe, thongKeObj.SoXe);
                                if (listCapNhienLieu != null && listCapNhienLieu.Count > 0)
                                    thongKeObj.SoDauTamUng = listCapNhienLieu.Sum(e => e.LuongNhienLieu);
                                var listNhienLieuTieuThu = ThongKeNhienLieuTieuThu.GetList(thang, nam, thongKeObj.IDTaiXe, thongKeObj.SoXe, TYPE_REPORT.BinhThuong);
                                if (listNhienLieuTieuThu != null && listNhienLieuTieuThu.Count > 0)
                                    thongKeObj.SoDauTieuThu = listNhienLieuTieuThu.Sum(e => e.TongLit);
                                var donGiaObj = DonGiaNhienLieuBLL.GetDonGia(thang, nam, thongKeObj.SoXe);
                                if (donGiaObj != null)
                                    thongKeObj.DonGia = donGiaObj.DonGia;
                                thongKeObj.ThanhTien = thongKeObj.DonGia * thongKeObj.SoDauConLai;
                                QuyetToan _qt = new QuyetToan
                                {
                                    DaQuyetToan = false,
                                    IDTaiXe = thongKeObj.IDTaiXe,
                                    Nam = thongKeObj.Nam,
                                    NgayQuyetToan = thongKeObj.NgayQuyetToan,
                                    SoDuDauKy = thongKeObj.SoDuDauKy,
                                    SoXe = thongKeObj.SoXe,
                                    Thang = thongKeObj.Thang,
                                    ThuTienMat = thongKeObj.TienMat,
                                };
                                db.QuyetToanDbSet.Add(_qt);
                                listThongKe.Add(thongKeObj);
                            }
                        }
                    }
                    db.SaveChanges();
                    return listThongKe;
                }
            }
        }

        public static decimal GetSoDuDauKy(int thang, int nam, int IDTaiXe, string soXe)
        {
            using (var db = DBContextHelper.GetContext())
            {
                thang--;
                if (thang == 0)
                {
                    thang = 12;
                    nam--;
                }
                var qt = db.QuyetToanDbSet.Where(e => e.Thang == thang && e.Nam == nam && e.IDTaiXe == IDTaiXe && e.SoXe == soXe).SingleOrDefault();
                if (qt != null)
                {
                    decimal soDauTamUng = 0, soDauTieuThu = 0, donGia = 0;
                    var listCapNhienLieu = ThongKeCapNhienLieu.GetList(thang, nam, IDTaiXe, soXe);
                    if (listCapNhienLieu != null && listCapNhienLieu.Count > 0)
                        soDauTamUng = listCapNhienLieu.Sum(e => e.LuongNhienLieu);
                    var listNhienLieuTieuThu = ThongKeNhienLieuTieuThu.GetList(thang, nam, IDTaiXe, soXe, TYPE_REPORT.BinhThuong);
                    if (listNhienLieuTieuThu != null && listNhienLieuTieuThu.Count > 0)
                        soDauTieuThu = listNhienLieuTieuThu.Sum(e => e.TongLit);
                    var donGiaObj = DonGiaNhienLieuBLL.GetDonGia(thang, nam, soXe);
                    if (donGiaObj != null)
                        donGia = donGiaObj.DonGia;
                    return qt.SoDuDauKy + qt.ThuTienMat + donGia * (soDauTieuThu - soDauTamUng);
                }
                else
                    return 0;
            }
        }

        /// <summary>
        /// Cập nhật tiền mặt
        /// </summary>
        /// <param name="thang"></param>
        /// <param name="nam"></param>
        /// <param name="IDTaiXe"></param>
        /// <param name="soXe"></param>
        /// <param name="tienMat"></param>
        /// <returns></returns>
        public static CODE_RESULT_RETURN UpdateTienMatOrSoDuDauKy(int thang, int nam, int IDTaiXe, string soXe, decimal tienMat, decimal soDuDauKy)
        {
            using (var db = DBContextHelper.GetContext())
            {
                var qt = db.QuyetToanDbSet.Where(e => e.Thang == thang && e.Nam == nam && e.IDTaiXe == IDTaiXe && e.SoXe == soXe).SingleOrDefault();
                if (qt != null)
                {
                    qt.SoDuDauKy = soDuDauKy;
                    qt.ThuTienMat = tienMat;
                    db.SaveChanges();
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                    return CODE_RESULT_RETURN.ThatBai;
            }
        }

        public static CODE_RESULT_RETURN QuyetToan(List<ThongKeTongHopThanhToan> list)
        {
            using (var db = DBContextHelper.GetContext())
            {
                foreach (var item in list)
                {
                    var qt = db.QuyetToanDbSet.Where(e => e.IDTaiXe == item.IDTaiXe && e.SoXe == item.SoXe && e.Thang == item.Thang && e.Nam == item.Nam).SingleOrDefault();
                    if (qt != null)
                        qt.DaQuyetToan = true;
                }
                db.SaveChanges();
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }
}
